package YAPclient;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import javax.swing.JTextField;



/**
 * This is a class for creating JTextFields that accept only a certain number of chars,
 * which is useful to limit user input
 */

public class LimitedTextField extends JTextField{
	

	private static final long serialVersionUID = 2835007614394417821L;
	
	public static final int FIELD_WIDTH = 10;
	
	public LimitedTextField (SetMaxText smt, String str, int width){
		super(smt, str, width);
	}
	
	public LimitedTextField(SetMaxText smt, String str){
		super(smt, str, FIELD_WIDTH);
	}

	public LimitedTextField(SetMaxText smt) {
		super(smt,"",FIELD_WIDTH);
	}
	
	public LimitedTextField(String str) {
		super(str, FIELD_WIDTH);
	}

}

class SetMaxText extends PlainDocument {

	private static final long serialVersionUID = -9153182082613296423L;
	
	private int limit;
	 
	SetMaxText(int limit) {
		super();
		this.limit = limit;
	}
	 
	public void insertString(int offset, String  str, AttributeSet attr) throws BadLocationException {
		if (str == null) return;
		if ((getLength() + str.length()) <= limit) {
			super.insertString(offset, str, attr);
		}
	}
	
}
